Linear Regression
Linear regression for high income countries
grp = income_grp[1]
dat2 <- get_data(dat, grp)
fit <- lm(EG.ELC.ACCS.ZS ~ year, data=dat2)
smdls[[grp]] <- fit
summary(fit)
##
## Call:
## lm(formula = EG.ELC.ACCS.ZS ~ year, data = dat2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -28.9039 0.1559 0.3959 0.6660 0.9061
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 39.379176 9.810203 4.014 6.15e-05 ***
## year 0.030007 0.004893 6.133 1.00e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.166 on 2462 degrees of freedom
## Multiple R-squared: 0.01505, Adjusted R-squared: 0.01465
## F-statistic: 37.62 on 1 and 2462 DF, p-value: 1.001e-09
pyear <- seq(min(dat2$year), 2020, 1)
plot(dat2, xlim = c(min(pyear), max(pyear)), ylim = c(0,100),
pch=16, col="blue", main = grp, xaxt='n',yaxt='n', cex = 0.5)
axis(1, at = seq(min(pyear), max(pyear), 1), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
axis(2, at = seq(0, 100, 5), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
matlines(pyear, predict(fit, newdata=list(year=pyear), interval="confidence"), lwd=1.25)

Linear regression for upper middle income countries
grp = income_grp[4]
dat2 <- get_data(dat, grp)
fit <- lm(EG.ELC.ACCS.ZS ~ year, data=dat2)
smdls[[grp]] <- fit
summary(fit)
##
## Call:
## lm(formula = EG.ELC.ACCS.ZS ~ year, data = dat2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -76.740 -2.563 4.487 7.295 14.611
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -660.41950 92.25268 -7.159 1.32e-12 ***
## year 0.37478 0.04596 8.154 7.87e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 13.93 on 1376 degrees of freedom
## Multiple R-squared: 0.04609, Adjusted R-squared: 0.0454
## F-statistic: 66.48 on 1 and 1376 DF, p-value: 7.872e-16
pyear <- seq(min(dat2$year), 2030, 1)
plot(dat2, xlim = c(min(pyear), max(pyear)), ylim = c(0,100),
pch=16, col="blue", main = grp, xaxt='n',yaxt='n', cex = 0.5)
axis(1, at = seq(min(pyear), max(pyear), 1), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
axis(2, at = seq(0, 100, 5), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
matlines(pyear, predict(fit, newdata=list(year=pyear), interval="confidence"), lwd=1.25)

Linear regression for lower middle income countries
grp = income_grp[3]
dat2 <- get_data(dat, grp)
fit <- lm(EG.ELC.ACCS.ZS ~ year, data=dat2)
smdls[[grp]] <- fit
summary(fit)
##
## Call:
## lm(formula = EG.ELC.ACCS.ZS ~ year, data = dat2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -55.556 -20.314 1.753 21.068 56.927
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.437e+03 1.855e+02 -13.14 <2e-16 ***
## year 1.246e+00 9.239e-02 13.49 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 26.15 on 1357 degrees of freedom
## Multiple R-squared: 0.1183, Adjusted R-squared: 0.1176
## F-statistic: 182 on 1 and 1357 DF, p-value: < 2.2e-16
pyear <- seq(min(dat2$year), 2036, 1)
plot(dat2, xlim = c(min(pyear), max(pyear)), ylim = c(0,100),
pch=16, col="blue", main = grp, xaxt='n',yaxt='n', cex = 0.5)
axis(1, at = seq(min(pyear), max(pyear), 1), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
axis(2, at = seq(0, 100, 5), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
matlines(pyear, predict(fit, newdata=list(year=pyear), interval="confidence"), lwd=1.25)

Linear regression for low income countries
grp = income_grp[2]
dat2 <- get_data(dat, grp)
fit <- lm(EG.ELC.ACCS.ZS ~ year, data=dat2)
smdls[[grp]] <- fit
summary(fit)
##
## Call:
## lm(formula = EG.ELC.ACCS.ZS ~ year, data = dat2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -30.026 -13.209 -5.467 10.185 77.288
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2090.9405 201.7137 -10.37 <2e-16 ***
## year 1.0536 0.1004 10.49 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 19.74 on 664 degrees of freedom
## Multiple R-squared: 0.1421, Adjusted R-squared: 0.1408
## F-statistic: 110 on 1 and 664 DF, p-value: < 2.2e-16
pyear <- seq(min(dat2$year), 2080, 1)
plot(dat2, xlim = c(min(pyear), max(pyear)), ylim = c(0,100),
pch=16, col="blue", main = grp, xaxt='n',yaxt='n', cex = 0.5)
axis(1, at = seq(min(pyear), max(pyear), 1), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
axis(2, at = seq(0, 100, 5), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
matlines(pyear, predict(fit, newdata=list(year=pyear), interval="confidence"), lwd=1.25)

Summary of linear regression with average values
pyear <- seq(1990, 2080, 1)
plot(x=pyear, y=c(), xlim = c(min(pyear), max(pyear)), ylim=c(0,100),
ylab = "EG.ELC.ACCS.ZS", xlab = "year", xaxt='n',yaxt='n')
axis(1, at = seq(min(pyear),max(pyear), 5), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
axis(2, at = seq(0, 100, 5), tck = 1, lty = 4, col = "lightgray", lwd = 0.75)
colors <- c("blue","red","orange","green")
names(colors) <- income_grp
for (grp in income_grp) {
gdat <- get_data(dat, grp, is.mean = T)
points(x=gdat$year, y=gdat$EG.ELC.ACCS.ZS, pch=16, col=colors[grp], xaxt='n', yaxt='n', cex=0.75)
matlines(pyear, predict(smdls[[grp]], newdata=list(year=pyear), interval="confidence"), lwd=1)
}
legend("bottomright", legend=c("high income","low income","lower middle income","upper middle income"),
col=colors, lty=0, cex = 0.75, bg = "transparent", pch=16, box.lty=0)
